<template>
  <div class="enshrine-comp-manage">
    <Button type="primary" @click="openEnshrineCompGroupForm('add')"><Icon type="md-add"/> 新建分组</Button>&nbsp;
    <div class="m-b-10px"></div>
    <Table :columns="columns" :data="tableData" :loading="loading"></Table>
    <Page size="small" class="m-t-5px float-right" :total="pageTotal" :current.sync="pageIndex" :page-size="pageSize"
          show-elevator show-total @on-change="loadEnshrineCompGroups">
    </Page>

    <EnshrineCompGroupForm ref="enshrineCompGroupForm"
                           @refreshDataList="loadEnshrineCompGroups"></EnshrineCompGroupForm>
  </div>
</template>

<script>

  import ManageMixin from "../../../../mixin/ManageMixin";
  import EnshrineCompGroupForm from "./EnshrineCompGroupForm";
  import EnshrineCompTableExpand from "./EnshrineCompTableExpand"

  export default {
    name: 'EnshrineCompManage',
    components: {EnshrineCompGroupForm},
    mixins: [ManageMixin],
    data() {
      return {
        enshrineCompGroupDrawerVisible: false,
        loading: false,
        columns: [
          {
            type: 'expand',
            width: 50,
            render: (h, params) => {
              return h(EnshrineCompTableExpand, {
                props: {
                  enshrineCompGroupId: params.row.id
                }
              })
            }
          },
          {
            title: '组名',
            key: 'group_name'
          },
          {
            title: '描述',
            key: 'remark'
          },
          {
            title: '排序',
            key: 'sort'
          },
          {
            title: '创建日期',
            key: 'create_date',
            width: 170
          },
          {
            title: '操作',
            key: 'action',
            width: 150,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('Button', {
                  props: {
                    size: 'small',
                    type: 'primary',
                    icon: 'md-create'
                  },
                  'class': {
                    'm-r-5px': true
                  },
                  on: {
                    click: () => {
                      this.openEnshrineCompGroupForm('edit', params.row.id)
                    }
                  }
                }),
                h('Poptip', {
                  props: {
                    confirm: true,
                    transfer: true,
                    'word-wrap': true,
                    width: 250,
                    placement: 'top-end',
                    title: '删除此分组，将同步删除组下的收藏组件，您确定要删除吗？'
                  },
                  style: {

                  },
                  on: {
                    'on-ok': () => {
                      this.deleteEnshrineCompGroup(params.row.id)
                    }
                  }
                }, [
                  h('Button', {
                    props: {
                      size: 'small',
                      type: 'error',
                      icon: 'md-trash'
                    }
                  },)
                ])
              ]);
            }
          }
        ]
      }
    },
    mounted() {
      this.loadEnshrineCompGroups()
    },
    methods: {

      loadEnshrineCompGroups () {
        this.loading = true
        this.$PnApi.EnshrineCompApi.getEnshrineCompGroupByPage(this.pageIndex, this.pageSize).then(result => {
          this.tableData = result.data.payload.list;
          this.pageTotal = result.data.payload.total;
          this.loading = false
        })
      },

      openEnshrineCompGroupForm (type, id) {
        this.$refs.enshrineCompGroupForm.init(type, id)
      },

      deleteEnshrineCompGroup (id) {
        this.$PnApi.EnshrineCompApi.deleteEnshrineCompGroup(id).then(result => {
          if(result.data.success) {
            this.loadEnshrineCompGroups();
            this.$Message.success(result.data.message)
          }
        })
      }

    },
    computed: {}
  }
</script>

<style scoped>
  >>> td.ivu-table-expanded-cell {
    padding: 10px 20px 10px 20px;
  }
</style>
